/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.cei.jpah2database;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Francisco
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2databasePU");
        EntityManager em = factory.createEntityManager();

        Persona p = new Persona();
        p.setNombre("pepeN");
        p.setApellido("pepeA");

        em.getTransaction().begin();
        em.persist(p);
        em.getTransaction().commit();

        List<Persona> lista = em.createQuery("SELECT p FROM Persona p").getResultList();

	System.out.println(lista.size());
        em.merge(p);

        Persona persona = em.find(Persona.class, 1); /*"", new PK(1,1) */
        
        try {
            persona = (Persona) em.createNamedQuery("buscarPersonaPorId").setParameter("idPersona", 0).getSingleResult();

            persona = (Persona) em.createNamedQuery("buscarPersonaPorNombre").setParameter("nombre", "pepeN").getSingleResult();
        } catch (NonUniqueResultException e) {
            e.printStackTrace();
        }

        em.remove(p);
        
        System.out.println(persona);
    }
}
